package com.changgou.order.controller;

import com.changgou.entity.CacheKey;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.pojo.OrderItem;
import com.changgou.pojo.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老大哥在看着你
 *
 * @author ANG.YU
 * @date 2019-10-22 15:50
 * 提供购物车数据
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkuFeign skuFeign;

    /**更新复选框选中状态*/
    @GetMapping("/updateChecked")
    void updateChecked(String skuId, boolean checked){
        String username = getUsername();
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CacheKey.CART_LIST + username).get(skuId);
        orderItem.setChecked(checked);
        redisTemplate.boundHashOps(CacheKey.CART_LIST + username).put(skuId, orderItem);
    }
    /**查询结算页面需要的商品列表和金额，数量*/
    @GetMapping("/findReadyCartList")
    Map findReadyCartList(){
        String username = getUsername();
        HashMap map = new HashMap();
        List<OrderItem> values = redisTemplate.boundHashOps(CacheKey.CART_LIST + username).values();
        Integer totalNum = 0;
        Integer totalPrice = 0;
        List<OrderItem> orderItemList = new ArrayList<>();
        for (OrderItem value : values) {
            if (value.isChecked()) {
                totalNum += value.getNum();
                totalPrice += value.getPrice() * value.getNum();
                orderItemList.add(value);
            }
        }
        /*将选中的商品列表保存到临时缓存中*/
        for (OrderItem orderItem : orderItemList) {
            String skuId = orderItem.getSkuId();
            redisTemplate.boundHashOps(CacheKey.CART_LIST_CATCH + username).put(skuId, orderItem);
        }
        map.put("orderItemList", orderItemList);
        map.put("totalNum", totalNum);
        map.put("totalPrice", totalPrice);

        return map;
    }

    @GetMapping("/findCartList")
    public Map findCartList(){
        String username = getUsername();
        HashMap map = new HashMap();
        List<OrderItem> values = redisTemplate.boundHashOps(CacheKey.CART_LIST + username).values();
        Integer totalNum = 0;
        Integer totalPrice = 0;
        map.put("orderItemList", values);

        for (OrderItem value : values) {
            if (value.isChecked()) {
                totalNum += value.getNum();
                totalPrice += value.getPrice() * value.getNum();
            }
        }
        map.put("totalNum", totalNum);
        map.put("totalPrice", totalPrice);

        return map;
    }

    @GetMapping("/addGoodsToCart")
    public void addGoodsToCart(String skuId,Integer num) {
        String username = getUsername();
        /*从Redis中获取sku的商品*/
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CacheKey.CART_LIST + username).get(skuId);
        if (null != orderItem) {
            /*购物车中已有该商品，在已有数量上追加*/
            orderItem.setNum(orderItem.getNum() + num);
            /*更新商品金额小记*/
            orderItem.setMoney(orderItem.getPrice() * orderItem.getNum());
            /*更新变更的商品数据*/
            redisTemplate.boundHashOps(CacheKey.CART_LIST + username).put(skuId, orderItem);
        } else {
            orderItem = new OrderItem();
            //1 商品数量
            orderItem.setNum(num);
            /*远程调用查询sku*/
            Sku sku = skuFeign.findSkuById(skuId);
            //2 图片
            orderItem.setImage(sku.getImage());
            //3 名称
            orderItem.setName(sku.getName());
            //4 单价
            orderItem.setPrice(sku.getPrice());
            //5 小计
            orderItem.setMoney(sku.getPrice() * num);
            //6 选中状态 新增商品默认是选中的
            orderItem.setChecked(true);
            //7 skuId
            orderItem.setSkuId(sku.getId());
            /*没有，直接添加就好*/
            redisTemplate.boundHashOps(CacheKey.CART_LIST + username).put(skuId,orderItem);
        }

    }
    public static String getUsername() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
      /*  System.out.println("name = " + name);*/
        return name;
    }
}
